package algotithm.leetcode.string.test821;

/**
 * @author 25068
 * @Date 2021/5/15 23:56
 * @Title 821. 字符的最短距离
 */
class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        String s = "loveleetcode";
        char c = 'c';
        int[] ans = solution.shortestToChar(s, c);
        for (int num : ans) {
            System.out.print(num + " ");
        }
    }

    public int[] shortestToChar(String S, char C) {
        int N = S.length();
        int[] ans = new int[N];
        int prev = Integer.MIN_VALUE / 2;

        for (int i = 0; i < N; ++i) {
            if (S.charAt(i) == C) {
                prev = i;
            }
            ans[i] = i - prev;
        }

        prev = Integer.MAX_VALUE / 2;
        for (int i = N-1; i >= 0; --i) {
            if (S.charAt(i) == C) {
                prev = i;
            }
            ans[i] = Math.min(ans[i], prev - i);
        }

        return ans;
    }
}


